﻿using Dapper;
using System;
using BCEntity.Common.Service.WebsiteServiceType;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Threading.Tasks;
using BCCommon;

namespace BCData.Common.Service.WebsiteServiceType
{
    /// <summary>
    /// 官网服务类型
    /// </summary>
    public class WebsiteServiceTypeData : IWebsiteServiceTypeData
    {
        private readonly IDatabaseContext databaseContext;
        private const string QUERY_EXISTS = @"SELECT 1 FROM websiteservicetype";
        private const string QUERY = @"SELECT WebsiteServiceTypeId,EditAdminId,EditAdminName,ServiceTypeName,Icon,Background,LinkUrl,Enabled,CreateAdminId,CreateAdminName,CreateTime,UpdateTime FROM websiteservicetype";
        public WebsiteServiceTypeData(IDatabaseContext databaseContext)
        {
            this.databaseContext = databaseContext;
        }
        #region 检查官网服务类型是否存在
        public async Task<bool> Exists(int websiteServiceTypeId)
        {
            string sql = string.Format(@"{0} WHERE WebsiteServiceTypeId=?WebsiteServiceTypeId", QUERY_EXISTS);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?WebsiteServiceTypeId", websiteServiceTypeId, DbType.Int64);
            return await this.databaseContext.Slave.ExecuteScalarAsync(sql, parameters) != null;
        }
        #endregion

        #region 添加官网服务类型
        public async Task<WebsiteServiceTypeEntity> Add(WebsiteServiceTypeEntity entity)
        {
            string sql = SqlCommands.GetInsertSql("websiteservicetype", new string[] { "ServiceTypeName", "Icon", "Background", "LinkUrl", "CreateAdminId", "CreateAdminName" });
            sql = string.Format("{0} {1} WHERE WebsiteServiceTypeId=LAST_INSERT_ID();", sql, QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?ServiceTypeName", entity.ServiceTypeName?.Trim(), DbType.AnsiString, size: 20);
            parameters.Add("?CreateAdminId", entity.CreateAdminId, DbType.Int64, size: 20);
            parameters.Add("?CreateAdminName", entity.CreateAdminName?.Trim(), DbType.AnsiString, size: 20);
            parameters.Add("?Icon", entity.Icon?.Trim(), DbType.AnsiString, size: 1000);
            parameters.Add("?Background", entity.Background?.Trim(), DbType.AnsiString, size: 1000);
            parameters.Add("?LinkUrl", entity.LinkUrl?.Trim(), DbType.AnsiString, size: 1000);
            return await this.databaseContext.Master.QueryFirstOrDefaultAsync<WebsiteServiceTypeEntity>(sql, parameters, databaseContext.Transaction);

        }
        #endregion

        #region 修改官网服务类型
        public async Task<WebsiteServiceTypeEntity> Update(WebsiteServiceTypeEntity entity)
        {
            string sql = SqlCommands.GetUpdateSql("websiteservicetype", new string[] { "EditAdminId", "EditAdminName", "ServiceTypeName", "Icon", "Background", "LinkUrl" }, new string[] { "WebsiteServiceTypeId" });
            sql = string.Format("{0} {1} WHERE WebsiteServiceTypeId=?WebsiteServiceTypeId;", sql, QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?WebsiteServiceTypeId", entity.WebsiteServiceTypeId, DbType.Int32, size: 11);
            parameters.Add("?EditAdminId", entity.EditAdminId, DbType.Int64, size: 20);
            parameters.Add("?EditAdminName", entity.EditAdminName?.Trim(), DbType.AnsiString, size: 20);
            parameters.Add("?ServiceTypeName", entity.ServiceTypeName?.Trim(), DbType.AnsiString, size: 20);
            parameters.Add("?Icon", entity.Icon?.Trim(), DbType.AnsiString, size: 1000);
            parameters.Add("?Background", entity.Background?.Trim(), DbType.AnsiString, size: 1000);
            parameters.Add("?LinkUrl", entity.LinkUrl?.Trim(), DbType.AnsiString, size: 1000);
            return await this.databaseContext.Master.QueryFirstOrDefaultAsync<WebsiteServiceTypeEntity>(sql, parameters, databaseContext.Transaction);
        }
        #endregion

        #region 删除官网服务类型
        public async Task<bool> Delete(int websiteServiceTypeId)
        {
            string sql = @"DELETE FROM websiteservicetype WHERE WebsiteServiceTypeId=?WebsiteServiceTypeId";
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?WebsiteServiceTypeId", websiteServiceTypeId, DbType.Int64);
            return await this.databaseContext.Master.ExecuteAsync(sql, parameters, databaseContext.Transaction) > 0;
        }
        #endregion

        #region 获取官网服务类型
        public async Task<WebsiteServiceTypeEntity> GetEntity(int websiteServiceTypeId)
        {
            string sql = string.Format(@"{0} WHERE WebsiteServiceTypeId=?WebsiteServiceTypeId", QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?WebsiteServiceTypeId", websiteServiceTypeId, DbType.Int64);
            return await this.databaseContext.Slave.QueryFirstOrDefaultAsync<WebsiteServiceTypeEntity>(sql, parameters);
        }
        #endregion

        #region 获取官网服务类型列表
        public async Task<IEnumerable<WebsiteServiceTypeEntity>> GetList()
        {
            string sql = string.Format(@"{0} WHERE Enabled=1", QUERY);
            return await this.databaseContext.Slave.QueryAsync<WebsiteServiceTypeEntity>(QUERY);

        }
        #endregion

        #region 获取官网服务类型分页
        public async Task<(IEnumerable<WebsiteServiceTypeEntity>, int)> Query(string serviceTypeName, DateTime? start, DateTime? end, string sortName, bool? ascending, int pageIndex, int pageSize)
        {
            List<string> wheres = new List<string>();
            DynamicParameters parameters = new DynamicParameters();
            if (!string.IsNullOrWhiteSpace(serviceTypeName))
            {
                wheres.Add("ServiceTypeName LIKE CONCAT('%',CONCAT(?ServiceTypeName,'%'))");
                parameters.Add("?ServiceTypeName", serviceTypeName?.Trim(), DbType.AnsiString);
            }
            if (start.HasValue && !end.HasValue)
            {
                wheres.Add("CreateTime>=?Start");
                parameters.Add("?Start", start.Value, DbType.DateTime);
            }
            else if (!start.HasValue && end.HasValue)
            {
                wheres.Add("CreateTime<=?End");
                parameters.Add("?End", end.Value, DbType.DateTime);
            }
            else if (start.HasValue && end.HasValue)
            {
                wheres.Add("CreateTime>=?Start AND CreateTime<=?End");
                parameters.Add("?Start", start.Value, DbType.DateTime);
                parameters.Add("?End", end.Value, DbType.DateTime);
            }
            string sql = string.Format(@"{0} {1} ORDER BY {4} {5} LIMIT {2},{3}; SELECT COUNT(1) FROM websiteservicetype {1}",
                  QUERY,
                  wheres.Count > 0 ? " WHERE " + string.Join(" AND ", wheres) : "",
                  (pageIndex == 0 ? 0 : pageIndex - 1) * pageSize,
                  pageSize,
                  string.IsNullOrWhiteSpace(sortName) ? "CreateTime" : sortName,
                  ascending.HasValue && ascending.Value ? "ASC" : "DESC");
            return await this.databaseContext.Slave.QueryMultipleAsync<WebsiteServiceTypeEntity, int>(sql, parameters);
        }
        #endregion

        #region 启用官网服务类型
        public async Task<bool> Enabled(int websiteServiceTypeId)
        {
            string sql = @"UPDATE websiteservicetype SET Enabled=1 WHERE WebsiteServiceTypeId=?WebsiteServiceTypeId";
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?WebsiteServiceTypeId", websiteServiceTypeId, DbType.Int32);
            return await this.databaseContext.Master.ExecuteAsync(sql, parameters, databaseContext.Transaction) > 0;
        }
        #endregion

        #region 禁用官网服务类型
        public async Task<bool> Disabled(int websiteServiceTypeId)
        {
            string sql = @"UPDATE websiteservicetype SET Enabled=0 WHERE WebsiteServiceTypeId=?WebsiteServiceTypeId";
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?WebsiteServiceTypeId", websiteServiceTypeId, DbType.Int32);
            return await this.databaseContext.Master.ExecuteAsync(sql, parameters, databaseContext.Transaction) > 0;
        }
        #endregion
    }
}



